package com.hys.controller;

import com.hys.dao.DepartmentDao;
import com.hys.dao.EmployeeDao;
import com.hys.pojo.Department;
import com.hys.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.Collection;

@Controller
public class EmployeeController {
    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;

    @RequestMapping("/emps")
    public String list(Model model){
        Collection<Employee> employees = employeeDao.getAll();
        model.addAttribute("emps",employees);
        return "/emp/list";
    }

    @GetMapping("/emp")
    public String toAddpage(Model model){
        //查出所有部门信息
        Collection<Department> departments = departmentDao.getDeparements();
        model.addAttribute("departments",departments);

        return "emp/add";
    }

    @PostMapping("/emp")
    public String addEmp(Employee employee){
        System.out.println("save>>"+employee);
        employeeDao.save(employee);
        return "redirect:/emps";
    }

    //来到修改页面，查出改员工的所有的信息;需要将员工的id信息也提交过来
    @GetMapping("/emp/{id}")
    public String toEditPage(@PathVariable("id") Integer id, Model model){
        Employee employee = employeeDao.getEmployeeById(id);
        model.addAttribute("emp",employee);
        System.out.println("这里是员工信息修改（先查出员工的信息）的模块"+employee);
        //修改页面要显示所有的部门信息进行修改的选择
        Collection<Department> departments = departmentDao.getDeparements();
        model.addAttribute("departments",departments);
        //回到修改页面（add是一个修改添加合为一体的界面）
        return "emp/add";
    }

    //员工修改信息
    @PutMapping("/emp")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    public String updateEmployee(Employee employee){
        System.out.println("修改的员工信息是"+employee.getBirth());
        employeeDao.save(employee);
        return  "redirect:/emps";
    }

    //删除员工
    @GetMapping("/delemp/{id}")
    public String deleteEmp(@PathParam("id") Integer id){
        employeeDao.delete(id);
        System.out.println("删除成功");
        return "redirect:/emps";
    }


}
